package 直方图中最大的矩形;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int N = 100010;
    static long[] h = new long[N];
    static long[] l = new long[N];
    static long[] r = new long[N];
    static int[] q = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String[] s = in.readLine().split(" ");
            int n = Integer.parseInt(s[0]);
            if (n == 0) {
                break;
            }
            for (int i = 1; i <= n; i++) {
                h[i] = Long.parseLong(s[i]);
            }
            int tt = 0;
            for (int i = 1; i <= n; i++) {
                while (tt > 0 && h[q[tt]] >= h[i]) {
                    tt--;
                }
                if (tt == 0) {
                    l[i] = 0;
                } else {
                    l[i] = q[tt];
                }
                q[++tt] = i;
            }
            tt = 0;
            for (int i = n; i >= 1; i--) {
                while (tt > 0 && h[q[tt]] >= h[i]) {
                    tt--;
                }
                if (tt == 0) {
                    r[i] = n + 1;
                } else {
                    r[i] = q[tt];
                }
                q[++tt] = i;
            }
            long res = 0;
            for (int i = 1; i <= n; i++) {
                res = Math.max(res, (r[i] - l[i] - 1) * h[i]);
            }
            System.out.println(res);
        }
    }
}
